﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BayesFileParser
{
    class BayesFileParser
    {
        private const int FILE_START_INDEX = 42;
        private const int NUM_QUESTIONS = 10;

        Dictionary<Category, List<string>> data;

        public BayesFileParser()
        {
            // string (category) -> list/array strings (options)
            data = new Dictionary<Category, List<string>>();
        }


        public void ReadFromCSVFile(string filename)
        {
            try
            {
                using (StreamReader sr = new StreamReader(filename))
                {
                    String line;
                    int lineCount = 0;

                    while ((line = sr.ReadLine()) != null)
                    {
                        if (lineCount != 0)
                        {
                            // We don't want to count the first line
                            // this is a CSV file -> split everything on commas
                            string[] answers = line.Split(',');

                            // start at index 39. go for 10 answers (<= index 48)
                            for (int i = FILE_START_INDEX;
                                i < FILE_START_INDEX + NUM_QUESTIONS && i < answers.Length;
                                i++)
                            {

                                // the second check is just to make sure we don't throw an error
                                // in the case of bad data


                                // get the category we are using. subtract (FILE_START_INDEX - 1) to get 
                                // a number starting at 1
                                Category first = Category.MetaCategoryNameToCategoryPair[(MetaCategoryName)
                                    (i - FILE_START_INDEX + 1)].first;
                                Category second = Category.MetaCategoryNameToCategoryPair[(MetaCategoryName)
                                    (i - FILE_START_INDEX + 1)].second;

                                // split the data up
                                string[] first_second = answers[i].Split('|');//.Substring(1, answers[i].Length - 2).Split('|');
                                
                                AddPhraseToCategory(first, first_second[0]);

                                if (first_second.Length > 1)
                                {
                                    AddPhraseToCategory(second, first_second[1]);
                                }

                            }
                        }

                        lineCount++;
                    }
                }
            }
            catch (NullReferenceException e)
            {
                throw new NullReferenceException("null reference exception: " + e.Message);
            }
            catch (Exception e)
            {
                //dunno if this is the right exception to throw. :P
                throw new FileLoadException(string.Format("Error accessing file: {0}", filename), e.Message);
            }


        }

        public void AddPhraseToCategory(Category cat, string phrase)
        {
            if (!this.data.ContainsKey(cat))
            {
                this.data.Add(cat, new List<string>());
            }

            this.data[cat].Add(phrase);
        }

        public Dictionary<Category, List<string>> BayesFileData
        {
            get { return this.data; }
        }

        // specify a directory?
        public void WriteToFiles(string directoryPath)
        {
            try
            {
                
                // Create a directory if one doesn't exist already
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }

                //Dictionary<Category, List<string>> data;
                foreach (Category cat in data.Keys)
                {
                    System.IO.File.WriteAllLines(getFilePath(cat, directoryPath), data[cat]);
                }
                
            }
            catch (Exception e)
            {
                Console.WriteLine("Writing to files failed: {0}", e.ToString());
            } 
        }

        public static string getFilePath(Category cat, string directoryPath)
        {
            //@"C:\Users\Public\TestFolder\WriteLines.txt"

            string filename = cat.Element.ToLower().Replace(' ', '_') + ".txt";
            return directoryPath + "/" + filename;
        }
    }
}
